/**
 *对象池 
 */
package utils.core
{
	import flash.utils.Dictionary;

	public class ObjectPool
	{
		private var groupDic:Dictionary;
		private static var i:ObjectPool;
		
		public function ObjectPool()
		{
			groupDic = new Dictionary();
		}
		
		private static function get I():ObjectPool
		{
			if(i == null)
				i = new ObjectPool();
			return i;
		}
		
		/**
		 *添加对象到对象池 
		 * @param obj：被添加的对象
		 * @param flag：分组标志
		 * 
		 */		
		public static function addObj(obj:Object, groupFlag:String):Boolean
		{
			var isSimpleType:Boolean;
			if(obj is int)
			{
				isSimpleType = true;
			}
			else if(obj is uint)
			{
				isSimpleType = true;
			}
			else if(obj is Number)
			{
				isSimpleType = true;
			}
			else if(obj is String)
			{
				isSimpleType = true;
			}
			else if(obj is XML)
			{
				isSimpleType = true;;
			}
			else if(obj is Boolean)
			{
				isSimpleType = true;;
			}
			if(isSimpleType)
			{
				throw new Error("简单数据不要缓存到对象池");
				return false;
			}
			
			
			if(I.groupDic[groupFlag] == null)
			{
				I.groupDic[groupFlag] = [];
			}
			I.groupDic[groupFlag].push(obj);
			return true;
		}
		
		//从groupFlag对象组中获取实例
		public static function getObj(groupFlag:String):Object
		{
			if(I.groupDic[groupFlag] == null)
				return null;
			if(I.groupDic[groupFlag].length == 0)
				return null;
			return I.groupDic[groupFlag].pop();
		}
		
		//删除对象组
		public static function deleteGroup(groupFlag:String):void
		{
			if(I.groupDic[groupFlag] == null)
				return;
			I.groupDic[groupFlag].length = 0;
			delete I.groupDic[groupFlag];
		}
		
	}
}